package server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class MimeTypeReader {

    private ArrayList<String[]> types = new ArrayList<String[]>();

    public MimeTypeReader() {
	try {
	    File file = new File( "mimetypes.txt" );
	    BufferedReader in = new BufferedReader( new FileReader( file ) );
	    String line = "";
	    while ( (line = in.readLine()) != null ) {
		System.out.println( "loading mime type " + line );
		String[] parts = line.split( "=" );
		types.add( parts );
	    }

	} catch ( FileNotFoundException e ) {
	    e.printStackTrace();
	} catch ( IOException e ) {
	    e.printStackTrace();
	}
	System.out.println( "Mime types loaded" );
    }

    public String getMimeType( String url ) {

	for ( String[] mime : types ) {
	    if ( url.endsWith( mime[0] ) )
		return mime[1];
	}
	return "";
    }

}
